

local model1 = [[KG_CJTX.mdl]]
ac.game:event '玩家-添加矿工'(function(_,player,gold)
    local gold = gold or 20
    local hero = player.hero
    local point = player.kc_point
    local angle = player.kc_angle 
    local count = player:get_data('矿工数量',0) + 1
    local point2 = point - {angle - 30*count,175}
    local angle2 = point2 / point
    --local level = math.ceil(count/2)
    local unit = ac.player[16]:create_unit('矿工动画',player.kg_point1)
    unit:add_effect(model1)
    unit.ui_title = '矿工'
    unit:issue_order('smart',point2)
    unit:add_restriction '无敌'
    unit:add_restriction '蝗虫'
    unit:add_buff '移动动画'{
        point = point2,
        angle = angle2,
        on_remove = function()
            unit:remove()
            local unit = player:create_unit('矿工',point2,angle2)
            unit:add_restriction '无敌'
            unit:add_restriction '幽灵'
            unit:set_owner(player)
            unit:add_animation_properties('gold')
            unit:loop(1000,function()
                unit:play_animation('attack')
                player:add_icon('金币',gold,unit)
            end)
            ac.game:event '游戏-关闭挑战'(function()
                unit:remove()
            end)
        end
    }
    player:set_data('矿工数量',count)
end)

ac.buff('移动动画'){
    pulse = 0.1,
    on_pulse = function(self)
        local unit = self.owner
        if unit:get_point()*self.point<=30 then
            self:remove()
        end
    end,
}

ac.game:event '玩家-添加矿场守护者'(function(_,player,index,time)
    local point = player.kg_point2
    local index = index or 1
    local sx = ('矿场守护者%s'):format(index)
    local unit = player:create_enemy('矿场守护者',point,sx)
    unit:add_buff '可挑战'{}
    if time then
        if ac.enemy.hard<3 then
            player:set_camera(point,0.5)
        end
        unit_add_life2(unit,1.5)
        unit:add_buff '假死复活'{
            time = time,
            on_remove = function()
                unit_add_life2(unit,2)
            end
        }
    else
        unit_add_life2(unit,2)
    end
    unit:set_facing(player.kc_angle)
    unit:event '单位-被挑战'(function(trg,_,source)
        unit:add_buff '挑战限时'{time = 20}
    end)
    unit:event '单位-即将移除'(function()
        if unit['被击败']==nil and ac.base_unit then
            player:notify('玩家-添加矿场守护者',player,index)
        end
    end)
    unit:event '单位-死亡'(function()
        unit['被击败'] = true
        player:notify('玩家-添加矿工',player)
        if index<4 then
            player:notify('玩家-添加矿场守护者',player,index + 1,180)
        end
        if player:get_data('引导-农民')==nil then
            player:set_data('引导-农民',true)
            player:notify('引导-显示内容',player,'农民','work！work！谢谢你解救了我，我将免费为您挖矿',[[ReplaceableTextures\CommandButtons\BTNPeasant.blp]])
        end
    end)
end)

ac.buff('可挑战')
{
    on_add = function(self)
        local unit = self.owner
        local player = unit.owner_player
        unit['可挑战'] = true
        unit.ui_title = ('|cffff0000双击挑战|r|n%s'):format(unit.name)
        self:gc(unit:get_point():add_effect([[9205c7daac61cafd83c7bc648d3754d2.mdl]]))
        unit:event '单位-被挑战'(function(trg,_,source)
            unit:issue_order('attack',source)
            self:remove()
        end)
        self:gc(unit:event '单位-被挑战'(function(trg,_,source)
            self:remove()
            player:set_data('引导-矿场守护者',true)
            player:notify('引导-显示内容',player)
        end))
        unit:add_restriction '免伤'
        unit:add_restriction '硬直'
        unit:add_restriction '无敌'
        self:gc(function()
            unit['可挑战'] = nil
            player:set_data('矿场守护者',unit)
            if self._move_timer then
                self._move_timer:remove()
            end
            unit:remove_restriction '免伤'
            unit:remove_restriction '硬直'
            unit:remove_restriction '无敌'
        end)
    end,
}


ac.sync:event '同步-放弃挑战'(function(_,player)
    local enemy = player:get_data('矿场守护者')
    if enemy then
        enemy:remove()
        player:set_data('矿场守护者',nil)
    end
end)

local function challenge_unit(player,target)
    return target and target['可挑战'] and target.owner_player == player
end

ac.wait(100,function()
    register_hero(function(player,hero)
        local unit = player:get_data('金矿')
        
        player.kc_point = unit:get_point()
        player.kc_angle = player.kc_point / player.point1

        ac.wait(100,function()
            player:notify('玩家-添加矿场守护者',player,1)
        end)

        hero:event '单位-发布命令'(function(_,_,id,target)
            local order = Order[id]
            if order=='attack' and challenge_unit(player,target) then
                target:notify('单位-被挑战',target,hero)
            end
        end)
        
        player._tz_time = 0
        player:event '玩家-选择单位'(function(_,_,target)
            if challenge_unit(player,target) and not target:find_buff '假死复活' then
                local clock = ac.clock()
                if clock-player._tz_time<0.4*1000 then
                    target:notify('单位-被挑战',target,hero)
                end
                player._tz_time = clock
            end
        end)
    end)
end)